class MUtil{
	request(param){
		return new Promise((resolve, reject) => {
			$.ajax({
				type 		: param.type 		|| 'get',
				url 		: param.url 		|| '',
				dataType 	: param.dataType 	|| 'json',
				data 		: param.data 		|| null,
				success     : res => {
					//登陆成功
					if (0 === res.status) {
						typeof resolve === 'function' && resolve(res.data,res.msg);
						//this.doLogin();
					} else if (10 === res.status) {
						this.doLogin();
					} else {
						typeof reject === 'function' && reject(res.msg || res.data);						
					}
				},
				error       : err => {
					typeof reject === 'function' && reject(err.statustext);						
				}
			});
		});
		
	}
	//跳转登录
	doLogin() {
		window.location.href = '/login?redirect' + encodeURIComponent(window.location.pathname);
	}
	//获取URL参数
	getUrlParam(name){
		// xxx.com?param=1234&value=3333
		let queryString = window.location.search.split('?')[1] || '',
			reg 		= new RegExp("(^|&)" + name + "=([^&]*)(&|$)"),
			result 		= queryString.match(reg);
			return result ? decodeURIComponent(result[2]) : null;
	}
	//错误提示
	errorTips(errMsg) {
		alert(errMsg || '好像哪里不对了~');
	}
	//添加本地存储
	setStorage(name,data){
		let dataType = typeof data;
		// json对象
		if (dataType === 'object') {
			window.localStorage.setItem(name,JSON.stringify(data));
		// 基础类型
		} else if(['number','boolean','string'].indexOf(dataType)) {
			window.localStorage.setItem(name,data);
		//其他类型
		} else {
			alert('该类型不能用于本地存储');
		}
	}
	//去除本地存储内容
	getStorage(name) {
		let data = window.localStorage.getItem(name);
		if (data) {
			return JSON.parse(data);
		} return '';
	}
	//删除本地存储内容
	removeStorage(name){
		window.localStorage.removeItem(name);
	}
}

export default MUtil;